package cn.lingyangwl.agile.file.excel;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.handler.inter.IExcelDictHandler;
import cn.lingyangwl.agile.common.core.utils.ExcelContentType;
import cn.lingyangwl.framework.core.utils.spring.SpringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author shenguangyang
 */
@Slf4j
@Component
public class CusExcelExportUtil {
    private static IExcelDictHandler excelDictHandler;

    @PostConstruct
    public void init() {
        try {
            excelDictHandler = SpringUtils.getBean(IExcelDictHandler.class);
        } catch (Exception e) {
            log.warn(e.getMessage());
        }
        if (Objects.isNull(excelDictHandler)) {
            log.warn("未发现表格导出导入字典处理器");
        }
    }

    public static <T> void exportExcel(HttpServletResponse response,
                            ExcelContentType excelContentType,
                            ExportParams exportParams,
                            String fileName,
                            Class<T> pojoClass, List<T> data) throws Exception {
        response.reset();
        try (Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, data);
             ServletOutputStream outputStream = response.getOutputStream()) {
            response.flushBuffer();
            response.setContentType(excelContentType.getContentType());
            response.setCharacterEncoding("utf-8");
            response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
            workbook.write(outputStream);
        }
    }

    public static <T> void exportExcel(HttpServletResponse response,
                                       ExportParams exportParams,
                                       String fileName,
                                       Class<T> pojoClass, List<T> data) throws Exception {
        response.reset();
        try (Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, data);
             ServletOutputStream outputStream = response.getOutputStream()) {
//            response.flushBuffer();
            response.setContentType("application/octet-stream");
            response.setCharacterEncoding("utf-8");
            response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
            workbook.write(outputStream);
        }
    }

    /**
     * 用于导出一个表格中多个sheet
     */
    public static <T> void exportExcel(HttpServletResponse response,
                                       List<Map<String, Object>> exportParams,
                                       String fileName, ExcelType excelType) throws Exception {
        response.reset();
        for (Map<String, Object> exportParam : exportParams) {
            ((ExportParams) exportParam.get("title")).setDictHandler(excelDictHandler);
        }
        try (Workbook workbook = ExcelExportUtil.exportExcel(exportParams, excelType);
             ServletOutputStream outputStream = response.getOutputStream()) {
            response.setContentType("application/octet-stream");
            response.setCharacterEncoding("utf-8");
            response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
            workbook.write(outputStream);
        }
    }
}
